// AWTAWTFormLayout.cpp: implementation of the AWTAWTFormLayout class.
//
//////////////////////////////////////////////////////////////////////

#include "stdafx.h"
#include "awt.h"
#include "AWTFormLayout.h"

#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif

IMPLEMENT_SERIAL(AWTFormLayout, AWTLayout, 1)

//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////

AWTFormLayout::AWTFormLayout()
{
	m_right = 0;
	m_gap = 2;
}

AWTFormLayout::~AWTFormLayout()
{

}

void AWTFormLayout::addLayoutComponent(CString* name, AWTComponent* comp)
{
}

void AWTFormLayout::removeLayoutComponent(AWTComponent* comp)
{
}

CSize AWTFormLayout::preferredLayoutSize(AWTContainer* target)
{
  AWTInsets insets = target->getInsets();
  CSize dim(0,0);
  int ncomponents = target->getComponentCount();
  AWTComponent* comp;
  CSize d(0,0);
	int left=0, right=0, height;

  int i = 0;
	for (; i < ncomponents ; i++) 
	{
		comp = target->getComponent(i);
		d = comp->getPreferredSize();
		if (i % 2)
		{
      right = max(d.cx, right);
			height = max(height, d.cy);
			dim.cy += height+m_gap;
		}
		else			
		{
      left = max(d.cx, left);
			height = d.cy;
		}
	}
	if (i % 2)
		dim.cy += height+m_gap;
	dim.cx = left + right;
	dim.cx += insets.left + insets.right + m_gap*2;
	dim.cy += insets.top + insets.bottom + m_gap*2;
	m_right = right;
  return dim;
}

CSize AWTFormLayout::minimumLayoutSize(AWTContainer* target)
{
  AWTInsets insets = target->getInsets();
  CSize dim(0,0);
  int ncomponents = target->getComponentCount();
  AWTComponent* comp;
  CSize d(0,0);
	int left=0, right=0, height;

  for (int i = 0 ; i < ncomponents ; i++) 
	{
		comp = target->getComponent(i);
		d = comp->getMinimumSize();
		if (i % 2)
		{
      right = max(d.cx, right);
			height = max(height, d.cy);
		}
		else			
		{
      left = max(d.cx, left);
			height = d.cy;
		}
		dim.cy += height+m_gap;
	}
	dim.cx = left + right;
	dim.cx += insets.left + insets.right + m_gap*2;
	dim.cy += insets.top + insets.bottom + m_gap*2;
  return dim;
}

void AWTFormLayout::layoutContainer(AWTContainer* target)
{
  AWTInsets insets = target->getInsets();
  int top = insets.top+m_gap;
  int left = 0;
  int height = 0;
  int ncomponents   = target->getComponentCount();
  AWTComponent* comp;
  CSize d(0,0);

	AWTTextField* tf = new AWTTextField(_T("Hello"));
	CSize sztf = tf->getPreferredSize();
	delete tf;

	int cxleft=0, cxright=0;
  for (int i = 0 ; i < ncomponents ; i++) 
	{
		comp = target->getComponent(i);
		d = comp->getPreferredSize();
		if (i % 2)
		{
      cxright = max(d.cx, cxright);
		}
		else			
		{
      cxleft = max(d.cx, cxleft);
		}
	}

  for (int i = 0 ; i < ncomponents ; i++) 
	{
		comp = target->getComponent(i);
		d = comp->getPreferredSize();
		if (i % 2)
		{
			left = cxleft + insets.left;
			height = max(height, d.cy);
		}
		else
		{
			left = insets.left;
			height = d.cy;
		}
		if (comp->IsKindOf(RUNTIME_CLASS(AWTLabel)))
			comp->setBounds(left, top + ((sztf.cy - d.cy) / 2), d.cx, d.cy);
		else
			comp->setBounds(left, top, d.cx, d.cy);
		if (i % 2)
			top += height+m_gap;
  }
}
/*
void AWTFormLayout::Serialize(CArchive& ar)
{
	AWTLayout::Serialize(ar);
	if (ar.IsStoring())
  {
		ar << (DWORD)m_gap;
		ar << (DWORD)m_right;
	}
  else
  {
		ar >> (DWORD&)m_gap;
		ar >> (DWORD&)m_right;
  }
}
*/